package org.example;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * cpu us使用率高的demo
 */
@RestController
@RequestMapping("/HighUsCpu")
public class HighUsCpuUsageDemo {


    @GetMapping("/test")
    public String test() {

        System.out.println("Starting CPU intensive task...");

        while (true) {
            // 此处放置一个无限循环，模拟高CPU占用
            doIntensiveCalculations();
        }
    }

//    public static void main(String[] args) {
//        System.out.println("Starting CPU intensive task...");
//
//        while (true) {
//            // 此处放置一个无限循环，模拟高CPU占用
//            doIntensiveCalculations();
//        }
//    }

    private static void doIntensiveCalculations() {
        double result = 0.0;
        for (int i = 0; i < 1000000; i++) {
            result += Math.random() * Math.random();
        }
    }
}
